#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time
import base64

from jujud import log
from jujud.tcptube import TCPChannel
from jujud.packet import Packet,depack,enpack

#######################################################################

SERVICE = 'ECHO'

#######################################################################

HOST = "127.0.0.1"
PORT = 54344

def main(options):
    if options.verbose :
        log.enableDebug()
    else :
        log.disableDebug()
    if options.times > 0 :
        times = options.times
    else :
        times = 10

    address = (HOST, PORT)
    c = TCPChannel(address)

    count_bytes = 0
    time_start = time.time()
    for k in range(times) :
        msg = ''.join(('#', str(k), ' hello, world.', 'A' * options.bytes))
        count_bytes += len(msg)
        pkt = Packet()
        pkt.service = SERVICE
        pkt.body = base64.b64encode(msg)
        pkt.setRequest()
        request,packid = enpack(pkt)

        if len(request) < 256 :
            log.debug('  GO: (size=%d)%s', len(request), `request`)
        else :
            log.debug('  GO: (size=%d)', len(request))
        c.sendRecord(request)

        response = c.recvRecord()
        if len(request) < 256 :
            log.debug('COME: (size=%d)%s', len(response), `response`)
        else :
            log.debug('COME: (size=%d)', len(response))
        pkt_response = depack(response)
    time_stop = time.time()
    time_last = time_stop - time_start
    if time_last == 0 : time_last = 0.001
    tps = times / time_last
    bindwidth = count_bytes / time_last
    if bindwidth > 1048576 :
        bindwidth = '%.2f MB/s' % (bindwidth / 1048576.0)
    elif bindwidth > 1024 :
        bindwidth = '%.2f KB/s' % (bindwidth / 1024.0)
    else :
        bindwidth = '%.2f B/s' % (bindwidth / 1.0)
    log.info('times:%d, time-last:%.2f seconds, tps=%.2f, bindwidth=%s', times, time_last, tps, bindwidth)

    del c
    # main()

if __name__ == '__main__' :
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help='show detail information')
    parser.add_option("", "--times", dest="times", type="int", default=10,
                        help='times of ping-pong between beater and echoer')
    parser.add_option("", "--bytes", dest="bytes", type="int", default=0,
                        help='bytes of message for ping-pong between beater and echoer')
    parser.add_option("", "--profile", dest="profile", action="store_true", default=False,
                        help='show profile information')
    (options, args) = parser.parse_args()
    if options.profile :
        prof_file = 'prof.beat.txt'
        import cProfile
        cProfile.run("main(options)", prof_file)
        import pstats
        p = pstats.Stats(prof_file)
        #p.sort_stats("time").print_stats()
        p.sort_stats("cumulative").print_stats()
    else :
        main(options)

    import sys
    sys.exit(0)
